//
//  ChooseGetMoneyTypeView.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/14.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "ChooseGetMoneyTypeView.h"
#import "ChooseGetServerTypeTableViewCell.h"

@interface ChooseGetMoneyTypeView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UIView *bgView;

@property (nonatomic, strong) UITableView *mainTableView; // 主框架
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) NSMutableArray *muDataArray;
@end

@implementation ChooseGetMoneyTypeView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backButton.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        [backButton addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        [self addSubview:self.bgView];
        
        __weak __typeof(self)weakSelf = self;
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(weakSelf);
            make.height.mas_equalTo(200 / HEIGHT_6S_SCALE);
        }];
        [self.bgView addSubview:self.mainTableView];
        [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(weakSelf);
            make.height.mas_equalTo(200 / HEIGHT_6S_SCALE);
        }];
    }
    return self;
}
#pragma mark -- init
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = getColor(whiteColor);
    }
    return _bgView;
}
#pragma mark -- show hidden
//出现
- (void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}
//消失
- (void)hidden{
    if (self.sfTypeBlock) {
        if ([self.muDataArray[0][@"imgSelected"] isEqualToString:@"1"]) {
            NSDictionary *dic = self.muDataArray[0];
            self.sfTypeBlock(dic);
        }
        if ([self.muDataArray[1][@"imgSelected"] isEqualToString:@"1"]) {
            NSDictionary *dic = self.muDataArray[1];
            self.sfTypeBlock(dic);
        }
    }
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.muDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseGetServerTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ChooseGetServerTypeTableViewCell class])];
    cell.dataDic = self.muDataArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75 / HEIGHT_6S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50 / HEIGHT_6S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        self.muDataArray[0][@"imgSelected"] = @"1";
        self.muDataArray[1][@"imgSelected"] = @"0";
    }
    if (indexPath.row == 1) {
        self.muDataArray[0][@"imgSelected"] = @"0";
        self.muDataArray[1][@"imgSelected"] = @"1";
    }
    [self.mainTableView reloadData];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    UILabel *titleLable = [[UILabel alloc]init];
    titleLable.backgroundColor = [UIColor clearColor];
    titleLable.textColor = getColor(blackColor);
    titleLable.font = DEF_FontSize_16;
    titleLable.textAlignment= NSTextAlignmentCenter;
    titleLable.text = @"选择收费方式";
    [headerView addSubview:titleLable];
    [titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(headerView);
    }];
    
    UIButton * exitBtn = [[UIButton alloc]init];
    exitBtn.backgroundColor = [UIColor clearColor];
    [exitBtn setImage:[UIImage imageNamed:@"btn_cha"] forState:UIControlStateNormal];
    [exitBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:exitBtn];
    [exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headerView.mas_centerY);
        make.right.equalTo(headerView.mas_right).offset(-10 / WIDTH_6S_SCALE);
        make.width.height.mas_equalTo(24 / WIDTH_6S_SCALE);
    }];
    
    UIView *lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = getColor(dividerColor);
    [headerView addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(headerView.mas_bottom).offset(1);
        make.right.left.equalTo(headerView);
        make.height.mas_equalTo(1 / WIDTH_6S_SCALE);
    }];
    
    return headerView;
}
- (void) btnAction:(UIButton *)btn
{
    [self hidden];
}
#pragma mark - init
- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaBottomHeight - SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _mainTableView.backgroundColor = [UIColor clearColor];
        _mainTableView.tableHeaderView = self.headerView;
        _mainTableView.tableFooterView = self.footerView;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 动态行高
        _mainTableView.rowHeight = UITableViewAutomaticDimension;
        // 预估行高
        _mainTableView.estimatedRowHeight = 80;
        [_mainTableView registerClass:[ChooseGetServerTypeTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ChooseGetServerTypeTableViewCell class])];
    }
    return _mainTableView;
}
- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
        _headerView.backgroundColor = [UIColor grayColor];
    }
    return _headerView;
}
- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}
- (NSMutableArray *)muDataArray
{
    if (!_muDataArray) {
        _muDataArray = @[@{@"chooseID":@"1",@"titleName":@"一口价",@"detailName":@"客户预约时一次性支付所有费用",@"imgSelected":@"0"}.mutableCopy,
                         @{@"chooseID":@"2",@"titleName":@"免费预约",@"detailName":@"客户预约时无需付款,完成服务后线上收取费用",@"imgSelected":@"0"}.mutableCopy
                         ].mutableCopy;
    }
    return _muDataArray;
}
@end
